/*
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see http://www.gnu.org/licenses/. 
 *
 *  Authors:
 *  	Albert Marieges Garcia
 *  	Álex Pardo Fernández
 *  	Enric Mayas Márquez
 *  	Juan Carlos Calvo Tejedor
 *  	Marc Gironés Dezsényi
 *  
 *  Contact:
 *  	bcn.tours.android@gmail.com
 *  Webpage:
 *  	bcntours.co.cc
 *
 */
package com.BCNTours.Vista;

import java.util.Vector;

import android.app.Activity;
import android.app.ListActivity;
import android.content.Intent;
import android.net.ParseException;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.ListView;

import com.BCNTours.R;
import com.BCNTours.Info.Vista.InfoActivity;
import com.BCNTours.Model.ApplicationData;
import com.BCNTours.Model.Attraction;
import com.BCNTours.Model.BasicInfoPoi;
import com.BCNTours.Model.InformationPoint;
import com.BCNTours.Model.Monument;
import com.BCNTours.Model.Museum;

/**
 * Displays all of the POIs corresponding to a single category.
 *
 */
public class CategoryList extends ListActivity {
	
    /*Atributions*/
    private Vector<RowData> data;
	private RowData rd;
	private CustomAdapter adapter;
	Bundle extras;
	private int tipo;
	
	private ApplicationData applicationData;
	
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.categories);
        
        extras = getIntent().getExtras();
        //image = extras.getInt("listImage");
        applicationData = (ApplicationData) extras.getSerializable("com.ShowMe.Model.PoiList");
        tipo = extras.getInt("tipo");
 
        //Animation of the screen and set the orientation the screen
        //setRequestedOrientation (ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
        LayoutInflater mInflater = (LayoutInflater) getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
        
        data = new Vector<RowData>();
        
        switch(tipo){
			//Attractions
			case 0:
				this.setTitle(getString(R.string.msgAttracions));
				for(int i = 0; i < applicationData.getAttractions().size(); ++i){
					try {
						rd = new RowData(i, applicationData.getAttractions().get(i).getName(), applicationData.getAttractions().get(i).getImage());
					} catch (ParseException e) {
						e.printStackTrace();
					}
					data.add(rd);
		    	}
	    		break;
			
	    	//Monuments
	    	case 1:
	    		this.setTitle(getString(R.string.msgMonuments));
	    		for(int i = 0; i < applicationData.getMonuments().size(); ++i){
					try {
						rd = new RowData(i, applicationData.getMonuments().get(i).getName(), applicationData.getMonuments().get(i).getImage());
					} catch (ParseException e) {
						e.printStackTrace();
					}
					data.add(rd);
		    	}
	    		break;
	    		
	    	//Museums
	    	case 2:    
	    		this.setTitle(getString(R.string.msgMuseums));
	    		for(int i = 0; i < applicationData.getMuseums().size(); ++i){
					try {
						rd = new RowData(i, applicationData.getMuseums().get(i).getName(), applicationData.getMuseums().get(i).getImage());
					} catch (ParseException e) {
						e.printStackTrace();
					}
					data.add(rd);
		    	}
	    		break;
	    		
	    	//Information points
	    	case 3:  
	    		this.setTitle(getString(R.string.msgInfoPoint));
	    		for(int i = 0; i < applicationData.getInformationPoints().size(); ++i){
					try {
						rd = new RowData(i, applicationData.getInformationPoints().get(i).getName(), applicationData.getInformationPoints().get(i).getImage());
					} catch (ParseException e) {
						e.printStackTrace();
					}
					data.add(rd);
		    	}
	    		break;
    	
	    	//All
	    	case 4:    	
	    		this.setTitle(getString(R.string.msgAll));
	    		for(int i = 0; i < applicationData.getBasicList().size(); ++i){
					try {
						@SuppressWarnings("unused")
						int a = 2130837529;
						rd = new RowData(i, applicationData.getBasicList().get(i).getName(), applicationData.getBasicList().get(i).getImage());
					} catch (ParseException e) {
						e.printStackTrace();
					}
					data.add(rd);
		    	}
	    		break;
	    		
        }
        
        adapter = new CustomAdapter(this, mInflater ,R.layout.favorites_list, R.id.title, data);
        setListAdapter(adapter);
        getListView().setTextFilterEnabled(true);
        
	}
	
	/**
     * The action listener of list
     */
    public void onListItemClick(ListView parent, View v, int positionPoi, long id) { 
    	
    	Intent intent = new Intent(getApplicationContext(), InfoActivity.class);
    	
    	Attraction att;
    	Monument mon;
    	Museum mus;
    	InformationPoint inf;
    	
    	intent.putExtra("unit", applicationData.getUnit());
    	
    	switch(tipo){
		//Attractions
		case 0:
			att = applicationData.getAttractions().get(positionPoi);
			intent.putExtra("com.ShowMe.Model.Attraction", att);
			intent.putExtra("tipo", tipo);
			break;
		//Monuments
		case 1:
			mon = applicationData.getMonuments().get(positionPoi);
			intent.putExtra("com.ShowMe.Model.Monument", mon);
			intent.putExtra("tipo", tipo);
			break;
		//Museums
		case 2:
			mus = applicationData.getMuseums().get(positionPoi);
			intent.putExtra("com.ShowMe.Model.Museum", mus);
			intent.putExtra("tipo", tipo);
			break;
    	//Information points
		case 3:
			inf = applicationData.getInformationPoints().get(positionPoi);
			intent.putExtra("com.ShowMe.Model.InformationPoint", inf);
			intent.putExtra("tipo", tipo);
			break;
    	//All
		case 4:
	    	BasicInfoPoi b = applicationData.getBasicList().get(positionPoi);
	    	if(b.getPoi() instanceof Attraction){
	    		att = (Attraction) b.getPoi();
				intent.putExtra("com.ShowMe.Model.Attraction", att);
				intent.putExtra("tipo", 0);
	    	} else if(b.getPoi() instanceof Monument){
	    		mon = (Monument) b.getPoi();
				intent.putExtra("com.ShowMe.Model.Monument", mon);
				intent.putExtra("tipo", 1);
	    	} else if(b.getPoi() instanceof Museum){
	    		mus = (Museum) b.getPoi();
				intent.putExtra("com.ShowMe.Model.Museum", mus);
				intent.putExtra("tipo", 2);
	    	} else if(b.getPoi() instanceof InformationPoint){
	    		inf = (InformationPoint) b.getPoi();
				intent.putExtra("com.ShowMe.Model.InformationPoint", inf);
				intent.putExtra("tipo", 3);
	    	}
	    	

		}
    	
    	startActivity(intent);
				
    	setListAdapter(adapter);
        getListView().setTextFilterEnabled(true);
    }
    
    /**
     * Create the menu of the intent
     */
    @Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.menufavorits, menu);
		return true;
    }
    
    /**
	 * Action listener of the search button
	 */
	@Override
	public boolean onSearchRequested() {
		Intent intent = new Intent(getBaseContext(), Search.class);
    	intent.putExtra("com.ShowMe.Model.PoiList", applicationData);
    	intent.putExtra("tipo", tipo);
    	startActivity(intent);
		setListAdapter(adapter);
        getListView().setTextFilterEnabled(true);
		return super.onSearchRequested();
	}
	
	/**
     * Action listener of the menu
     */
    public boolean onOptionsItemSelected(MenuItem item){
    	switch(item.getItemId()){
    		case R.id.menuSearch:
    			
    			Intent intent = new Intent(getBaseContext(), Search.class);
    	    	intent.putExtra("com.ShowMe.Model.PoiList", applicationData);
    	    	intent.putExtra("tipo", tipo);
    	    	startActivity(intent);
    			setListAdapter(adapter);
    	        getListView().setTextFilterEnabled(true);
    			break;
    	}
    	return false;
    }
}
